<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>注册页面</h1>
    <!--blur代表失去焦点事件-->
    <input type="text" v-model="user.username" placeholder="请输入用户名" @blur="f()">{{info}}<br>
    <input type="text" v-model="user.password" placeholder="请输入密码"><br>
    <input type="text" v-model="user.nick" placeholder="请输入昵称"><br>
    <input type="button" value="注册" @click="reg()" v-bind:disabled="info=='用户名已存在'">
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            info: "",
            user: {
                username: "",
                password: "",
                nick: ""
            }
        },
        methods: {
            reg() {
                axios.post("/reg", v.user).then(function (response) {
                    if (response.data == 1) {
                        alert("注册成功!")
                        //修改浏览器请求地址
                        location.href = "/";//显示到首页
                    } else {
                        alert("用户名已存在")
                    }
                })
            },
            f() {
                console.log("编辑完成")
                axios.get("/check?username=" + v.user.username).then(function (response) {
                    v.info = response.data == 1 ? "用户名可用" : "用户名已存在";
                })
            }
        }
    })
</script>
</body>
</html>